package demo6;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.lang.reflect.Constructor;

/*
* 反射机制可以破除单例模式
* */
public class Demo3 {
    public static void main(String[] args) throws Exception {

        Class cls = Class.forName("demo6.Student");//获取类对象
        /*
        *Class对象的newInstance()方法只能调用公开无参的构造器，
        * Object obj = cls.newInstance();
        * System.out.println(obj);
        * */

        // 获取私有构造器 并设置访问权限 通过类的对象获取Student的私有无参构造器
        Constructor constructor = cls.getDeclaredConstructor();
        constructor.setAccessible(true);//设置访问权限
        Object s1 = constructor.newInstance();
        Object s2 =  constructor.newInstance();
        System.out.println(s1);
        System.out.println(s2);
        System.out.println(s1 == s2);
    }
}
